package org.review.datastructure;

import java.util.Scanner;

/**
 * 数组实现环形队列
 * step 1:  考虑 什么时候队列空  头尾相等 front = rear      这个比较好理解
 * step 2:  考虑 什么时候队列满  (rear +1 ) % maxSize  = front  是因为front指向下一个, rear也指向下一个, 这样就需要rear+1  再取模 用一个空位置座位约定
 * step 3:  有效元素个数 (rear+maxSize-front)%maxSize    : maxSize +rear  是rear 已经循环一周后 小于front 的时候使用
 * step 4:  添加元素的时候如果队列未满,queue[rear]后 需要rear=(rear+1)%maxSize  解决当rear =maxSize-1时候模拟环 否则就越界了
 * step 5: 同step4
 */
public class RingQueue {
    private int maxSize;
    private int front;
    private int rear;
    private int[] arrayQueue;

    public static void main(String[] args) {
        RingQueue queue = new RingQueue(3);
        char key = ' '; //接收用户输入
        Scanner scanner = new Scanner(System.in);//
        boolean loop = true;
        //输出一个菜单
        while (loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key = scanner.next().charAt(0);//接收一个字符
            switch (key) {
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("输出一个数");
                    int value = scanner.nextInt();

                    queue.addElement(value);
                    break;
                case 'g': //取出数据
                    try {
                        int res = queue.getElement();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h': //查看队列头的数据
                    try {
                        int res = queue.headQueue();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e': //退出
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
    }

    public RingQueue(int maxSize) {
        this.maxSize = maxSize;
        arrayQueue = new int[maxSize];
    }

    public boolean isFull() {
        return (rear+1) % maxSize == front;
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public int size() {
        return (rear + maxSize - front) % maxSize;
    }

    public void addElement(int element) {
        if (isFull()) {
            System.out.println("队列满，不能加入数据~");
            return;
        }
        arrayQueue[rear] = element;
        rear = (rear + 1) % maxSize;
    }

    public int getElement() {
        if (isEmpty()) {
            throw new RuntimeException("队列空，不能取数据");
        }

        int element = arrayQueue[front];
        front = (front + 1) % maxSize;
        return element;
    }

    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列空的，没有数据~~");
            return;
        }
        for (int i = front; i < front + size(); i++) {
            System.out.printf("queArray[%d] = %d\n", i % maxSize, arrayQueue[i % maxSize]);
        }
    }

    public int headQueue() {
        if (isFull()) {
            System.out.println("队列空的，没有数据~~");
            return -1;
        }
        return arrayQueue[front];
    }
}
